import json
import time
import requests
from fake_useragent import UserAgent


# 获取代理
def get_ua():
    common_desktop_and_mobile = ['chrome', 'firefox', 'safari', 'edge', 'opera']
    while True:
        user_agent = UserAgent(os=["windows", "macos"],platforms=["pc"]).random
        if any(device in user_agent.lower() for device in common_desktop_and_mobile):
            return user_agent

def get_proxy1(country="US"):
    ip = requests.get(f"https://api.922proxy.com/api/extract_ip?regions={country}&num=1&protocol=socks5&type=txt&lt=1")
    pass #print("get_good_proxy : ",ip.text)
    return ip.text.strip()

def get_proxy2(country="US"):
    ip = requests.get(f"https://info.proxy.abcproxy.com/extractProxyIp?regions={country}&num=1&protocol=http&return_type=txt&lh=1&mode=1")
    pass #print(ip.text)
    return ip.text.strip()

def get_proxy3(country="US"):
    res = requests.get(f"https://tq.lunaproxy.com/getflowip?neek=1408580&num=1&regions={country}&ip_si=2&level=1&sb=")
    # pass #print("get_good1_proxy", ip.text)
    ip, port = res.text.strip().split(":")
    return {"ip": ip, "port": port}
    


def get_proxy4(country="US"):
    # https://www.roxlabs.cn/ucenter/info/
    tiqu = f'http://api.proxy.roxlabs.io/getProxyIp?num=1&return_type=json&lb=4&sb=&flow=1&regions={country}&protocol=socks'

    i = 0
    while i < 5:
        # 每次提取10个，放入线程中
        resp = requests.get(url=tiqu, timeout=5)
        try:
            if resp.status_code == 200:
                dataBean = json.loads(resp.text)
            else:
                i += 1
                pass #print("获取失败")
                time.sleep(1)
                continue
        except ValueError:
            i += 1
            pass #print("获取失败")
            time.sleep(1)
            continue
        else:
            # 解析json数组，获取ip和port
            pass #print("code=", dataBean["code"])
            code = dataBean["code"]
            if code == 0:
                for proxy in dataBean["data"]:
                    return proxy  # {'ip': '43.153.97.48', 'port': 10448}
        i += 1
        pass #print(i)


def get_proxy5(country="US"):
    url = f"http://api.proxy.ipidea.io/getBalanceProxyIp?num=1&return_type=json&lb=1&sb=0&flow=1&regions={country}&protocol=socks"
    i = 0
    while i < 5:
        # 每次提取10个，放入线程中
        resp = requests.get(url=url, timeout=5)
        try:
            if resp.status_code == 200:
                dataBean = json.loads(resp.text)
                pass #print(dataBean)
            else:
                i += 1
                pass #print("获取失败")
                time.sleep(1)
                continue
        except ValueError:
            i += 1
            pass #print("获取失败")
            time.sleep(1)
            continue
        else:
            # 解析json数组，获取ip和port
            pass #print("code=", dataBean["code"])
            code = dataBean["code"]
            if code == 0:
                for proxy in dataBean["data"]:
                    return proxy  # {'ip': '43.153.97.48', 'port': 10448}
        i += 1
        pass #print(i)


def get_proxy6(country="US"):
    # https://www.ipwo.net/user/ucenter/api_extraction 
    url = f"https://www.ipwo.net/api/proxy/get_proxy_ip?num=1&regions={country}&protocol=socks5&return_type=json&lb=4&sb="
    i = 0
    while i < 5:
        # 每次提取10个，放入线程中
        resp = requests.get(url=url, timeout=5)
        try:
            if resp.status_code == 200:
                dataBean = json.loads(resp.text)
                pass #print(dataBean)
            else:
                i += 1
                pass #print("获取失败")
                time.sleep(1)
                continue
        except ValueError:
            i += 1
            pass #print("获取失败")
            time.sleep(1)
            continue
        else:
            # 解析json数组，获取ip和port
            pass #print("code=", dataBean["code"])
            code = dataBean["code"]
            if code == 0:
                for proxy in dataBean["data"]:
                    return proxy  # {'ip': '43.153.97.48', 'port': 10448}
        i += 1
        pass #print(i)


def get_proxy7():
    # https://app.ipfoxy.net/ipServiceList/ipDynamic, 适用于指纹浏览器
    import http.client

    conn = http.client.HTTPSConnection("apis.ipfoxy.com")

    headers = {
        'api-token': "153c4cf137abd8d20b4710058b3dd3a0",
        'api-id': "i36vc3v"
    }

    conn.request("GET", "/ip/open-api/area-list", headers=headers)

    res = conn.getresponse()
    data = res.read()

    pass #print(data.decode("utf-8"))


# https://www.lunaproxy.com/hk/my_account/??p=1
# cr07dm@163.com
# DI3vOkwzG

# https://www.roxlabs.cn/ucenter/info/
# 18600205747
# DI3vOkwzG

# https://app.ipfoxy.net/ipServiceList/ipDynamic
# cr07dm@163.com
# DI3vOkwzG

# https://www.ipidea.net/ucenter/
# 18600205747
# DI3vOkwzG

# https://www.ipwo.net/user/ucenter/api_extraction
# heshifei666   
# heshifei666

# 获取url
# https://www.roxlabs.cn/ucenter/ip_manage/

# 判断url正确性

# 添加进程池，用来完成每个应用
# pass #print(get_proxy5())
